package com.bigdata.movieticketapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.entity.Charge;
import com.bigdata.movieticketapi.entity.User;
import com.bigdata.movieticketapi.mapper.ChargeMapper;
import com.bigdata.movieticketapi.mapper.UserMapper;
import com.bigdata.movieticketapi.service.IChargeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.CreateChargeReqVO;
import com.bigdata.movieticketapi.vo.req.DoChargeReqVO;
import com.bigdata.movieticketapi.vo.req.PageChargeReqVO;
import com.bigdata.movieticketapi.vo.resp.PageChargeRespVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 充值表 服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-11-15
 */
@Service
public class ChargeServiceImpl extends ServiceImpl<ChargeMapper, Charge> implements IChargeService {

    @Resource
    private ChargeMapper chargeMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public void createCharge(CreateChargeReqVO vo) {
        Charge charge = new Charge();
        charge.setAmount(vo.getAmount());
        charge.setStatus(0);
        int result = chargeMapper.insert(charge);
        if (result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Transactional
    @Override
    public void doCharge(DoChargeReqVO vo) {
        Charge charge = chargeMapper.selectById(vo.getId());
        if (charge == null) {
            throw new BusinessException(ResponseCode.CHARGE_INVALID_EXISTS.getCode(),ResponseCode.CHARGE_INVALID_EXISTS.getMessage());
        }
        if(charge.getStatus() == 1){
            throw new BusinessException(ResponseCode.CHARGE_INVALID_EXISTS.getCode(),ResponseCode.CHARGE_INVALID_EXISTS.getMessage());
        }
        charge.setStatus(1);
        int chargeUpdate = chargeMapper.updateById(charge);
        if (chargeUpdate !=1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
        User user = userMapper.selectById(vo.getUserId());
        BigDecimal wallet = user.getWallet();
        user.setWallet(wallet.add(charge.getAmount()));
        int updateResult = userMapper.updateById(user);
        if (updateResult !=1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageChargeRespVO> pageCharges(PageChargeReqVO vo) {
        IPage<PageChargeRespVO> result = chargeMapper.selectPageCharge(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }
}
